#ifndef CFDL_PTL_IMPL_H
#define CFDL_PTL_IMPL_H

#include <stdint.h>
#include <unordered_map>

#include "cfdl_ptl_type.h"
#include "dev_ptl_model.h"

class CFDLPtlImpl : public DevPtlModel
{
public:
    explicit CFDLPtlImpl();
    virtual ~CFDLPtlImpl();

public:
    virtual void ptl_run(uint16_t ms);
    virtual int  ptl_io_in(uint8_t *data, uint16_t len);
    virtual void register_send_fun(std::function<void(const char*, uint16_t)>);

public:
    virtual void register_cmd_module(uint16_t, cfdl_msg_fun_cb, cfdl_msg_fun_cb);

public:
    int  ptl_io_out(cfdl_message_t *msg);

    void clr_heart_send(); //清除心跳发送时间

private:
    void cmd_module_exec(cfdl_message_t*);
    void cmd_module_exec_err(cfdl_message_t*);

    int ptl_io_in_parse(uint8_t* data, int len);
    void broadcast_heart();

    void __cfdl_err_(int,uint16_t);
    void cfdl_ro_send_heartbeat(uint8_t type,CFID id,uint8_t frame_type);

private:
    uint8_t  _send_seq = 0;
    uint32_t _heart_tick = 0;
    uint32_t _ptl_tick   = 0;

    uint8_t cfdl_chid = 1;
    cfdl_io_node_t io_node_list;
    cfdl_heartbeat_t heartbeat;

    std::unordered_map<int, cmd_module_node*> _cmd_map;
    std::function<void(const char*, uint16_t)> _send_data = nullptr;
};

#endif // CFDL_PTL_IMPL_H
